﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Elektronix.Class
{
    public class categories
    {
  
        public int categoryID { get; set; }
        public string categoryname { get; set; }
        public string description { get; set; }


        public static List<categories> categorigetir()
        {
            List<categories> cat = new List<categories>();
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("select * from categories",sqlcon);
            sqlcon.Open();
            SqlDataReader sdr = sqlcmd.ExecuteReader();
            while (sdr.Read())
            {
                categories c = new categories();
                c.categoryID = Convert.ToInt32(sdr["categoryID"]);
                c.categoryname = sdr["categoryname"].ToString();
                c.description = sdr["description"].ToString();
                cat.Add(c);
            }
            sqlcon.Close();
            return cat;
        }


        public void kategoriekle()
        {
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("insert into categories values (@categoryname,@description)", sqlcon);
            sqlcmd.Parameters.AddWithValue("@categoryname", categoryname);
            sqlcmd.Parameters.AddWithValue("@description", description);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }


        public void kategorisil()
        {
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("delete from categories where categoryID=@categoryID", sqlcon);
            sqlcmd.Parameters.AddWithValue("@categoryID", categoryID);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }



        public void kategoriguncelle()
        {
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("update categories set categoryname=@cn,description=@des where categoryID=@categoryID", sqlcon);
            sqlcmd.Parameters.AddWithValue("@categoryID", categoryID);
            sqlcmd.Parameters.AddWithValue("@cn", categoryname);
            sqlcmd.Parameters.AddWithValue("@des", description);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }




        public void tekkategorigetir()
        {
         
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("select * from categories where categoryID=@cid", sqlcon);
            sqlcmd.Parameters.AddWithValue("@cid", categoryID);
            sqlcon.Open();
            SqlDataReader sdr = sqlcmd.ExecuteReader();
            while (sdr.Read())
            {
                
              
                categoryname = sdr["categoryname"].ToString();
                description = sdr["description"].ToString();
          
            }
            sqlcon.Close();
         
        }



        public static List<categories> categorigetirHepsi()
        {
            List<categories> cat = new List<categories>();
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("select * from categories",sqlcon);
            sqlcon.Open();
            SqlDataReader sdr = sqlcmd.ExecuteReader();
            while (sdr.Read())
            {
                categories c = new categories();
                c.categoryID = Convert.ToInt32(sdr["categoryID"]);
                c.categoryname = sdr["categoryname"].ToString();
                c.description = sdr["description"].ToString();
                cat.Add(c);
            }


            sqlcon.Close();

            categories d = new categories();
            d.categoryname = "Hepsi";
            cat.Add(d);

            return cat;
        }
        


    }
}